<template>
  <div id="home">
    <!-- 头部区域 -->
    <div class="home-container">

      <van-nav-bar fixed>
        <!-- 左侧的插槽 -->
        <template #left>
          <img src="../assets/toutiao_logo.png" alt="logo" class="logo" />
        </template>
        <!-- 右侧的插槽 -->
        <template #right>
          <van-icon name="search" color="white" size="18" @click="$router.push('/search')" />
        </template>
      </van-nav-bar>
    </div>
    <!-- end -->
    <!-- 频道列表的标签页 -->
    <van-tabs v-model="active" sticky offset-top="1.22666667rem">
      <van-tab v-for="item in userChannel" :key="item.id" :title="item.name">
        <ArtList :channelId="item.id"></ArtList>
      </van-tab>
    </van-tabs>
    <!-- end -->
    <!-- 频道管理的小图标 -->
    <van-icon name="plus" size="16" class="plus" @click="show = true" />
    <!-- end -->
    <!-- 频道管理的弹出层 -->
    <van-popup v-model="show" :close-on-click-overlay="false" @closed="isDel = false">
      <div class="popup-container">
        <!-- 弹出层的头部区域 -->
        <van-nav-bar title="频道管理">
          <template #right>
            <van-icon name="cross" size="14" color="white" @click="show = false" />
          </template>
        </van-nav-bar>

        <!-- 弹出层的主体区域 -->
        <div class="pop-body">
          <!-- 我的频道 -->
          <div class="my-channel-box">
            <div class="channel-title">
              <div>
                <span class="title-bold">已添加频道：</span>
                <!-- 提示的文本 -->
                <span class="title-gray">{{ isDel ? '点击移除频道' : '点击进入频道' }}</span>
              </div>
              <!-- 按钮的文本 -->
              <span class="btn-edit" @click="isDel = !isDel">{{ isDel ? '完成' : '编辑' }}</span>
            </div>
            <!-- 我的频道列表 -->
            <van-row type="flex">
              <van-col span="6" v-for="(item, index) in userChannel" :key="item.id">
                <!-- 用户的频道 Item 项 -->
                 <div class="channel-item van-hairline--surround" @click="onUserChannelClick(item, index)">
                  {{item.name}}
                  <!-- 删除的图标 -->
                 <van-badge color="transparent" class="cross-badge" v-if="isDel && item.name !== '推荐' && userChannel.length > 2">
                    <template #content>
                      <van-icon name="cross" class="badge-icon" color="#cfcfcf" size="12" />
                    </template>
                  </van-badge>
                </div>
              </van-col>
            </van-row>
          </div>

          <!-- 分隔线 -->
          <div class="van-hairline--top sp-line"></div>

          <!-- 更多频道 -->
          <div class="more-channel-box">
            <div class="channel-title">
              <div>
                <span class="title-bold">可添加频道：</span>
                <span class="title-gray">点击添加频道</span>
              </div>
            </div>
            <!-- 更多频道列表 -->
            <van-row type="flex">
              <van-col span="6" v-for="item in moreChannels" :key="item.id">
                <div class="channel-item van-hairline--surround" @click="addChannel(item)">{{item.name}}</div>
              </van-col>
            </van-row>
          </div>
        </div>
      </div>
    </van-popup>
    <!-- end -->

  </div>
</template>

<script>
  // 按需导入 API 接口
  import {
    getUserChannelAPI,
    getAllChannelAPI,
    updateUserChannelAPI
  } from '../api/homeAPI.js'
  // 文章列表组件
  import ArtList from '../components/ArtileList/ArtileList.vue'
  export default {
    name: 'Home',
    data() {
      return {
        active: 0,
        // 用户的频道列表数组
        userChannel: [],
        show: false,
        // 所有的频道列表数据
        allChannel: [],
        isDel: false
      }
    },
    methods: {
      async initUserChannel() {
        // 1. 调用 API 接口
        const {
          data: res
        } = await getUserChannelAPI()
        // 2. 判断请求是否成功
        if (res.message === 'OK') {
          // 3. 为用户的频道列表赋值
          this.userChannel = res.data.channels
          // console.log(this.userChannel)
        }
      },
      async initAllChannelAPI() {
        const {
          data: res
        } = await getAllChannelAPI()
        if (res.message === 'OK') {
          //
          this.allChannel = res.data.channels
        }
      },
      async updateChannel(item) {
        
        // 1. 准备要提交到服务器的数据
        const channels = this.userChannel
          .filter(item => item.name !== '推荐') // 将“推荐”从频道列表中过滤出去
          .map((item, index) => { // 调用数组的 map 循环，最终返回一个处理好的新数组
            return {
              id: item.id,
              seq: index + 1
            }
          })
          
        // 2. 调用 API 接口
        
        const {
          data: res
        } = await updateUserChannelAPI(channels)
        console.log('111',res)
        if (res.message === "更新用户频道成功") {
          
          // TODO：提示用户更新成功
          // 3. 通过 notify 弹框提示用户更新成功
          console.log('1')
          this.$notify({
            type: 'success',
            message: '更新成功',
            duration: 1000
          })
        }
      },
      addChannel(item) {
        this.userChannel.push(item)
        this.updateChannel(item)
      },
      // 从用户频道列表中，移除指定 id 的频道
      onUserChannelClick(channel,index){
        if(this.isDel){
          if (channel.name === '推荐' || this.userChannel.length === 2) return
          // 处于删除状态
                // TODO1：从 userChannel 中移除指定的频道
                // TODO2：将更改过后的用户频道数据，提交到服务器保存
                this.userChannel = this.userChannel.filter(item => item.id !== channel.id)
                this.updateChannel()
        }else{
          // 1. 修改 Tabs 的激活项的索引值
              this.active = index
              // 2. 关闭 popup 弹出层
              this.show = false
        }
      }
    },
    created() {
      //请求用户列表数据
      this.initUserChannel()

      // 请求所用的频道列表数据
      this.initAllChannelAPI()
    },
    components: {
      ArtList
    },
    computed: {
      //
      moreChannels() {
        //
        return this.allChannel.filter(item => {
          const index = this.userChannel.findIndex(x => x.id === item.id)
          if (index === -1) return true
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  /* // 组件外层容器的样式 */
  .home-container {
    padding-top: 46px;
    // padding-bottom: 50px;
  }

  /* // logo 样式 */
  .logo {
    height: 80%;
  }

  // 为 tabs 容器设置右 padding
  /deep/ .van-tabs__wrap {
    padding-right: 36px;
    background-color: white;
  }

  // 频道管理小图标的定位
  .plus {
    position: fixed;
    top: 58px;
    right: 10px;
    z-index: 999;
  }

  .van-popup,
  .popup-container {
    background-color: transparent;
    height: 100%;
    width: 100%;
  }

  .popup-container {
    display: flex;
    flex-direction: column;
  }

  .pop-body {
    flex: 1;
    overflow: scroll;
    padding: 8px;
    background-color: white;
  }

  .my-channel-box,
  .more-channel-box {
    .channel-title {
      display: flex;
      justify-content: space-between;
      font-size: 14px;
      line-height: 28px;
      padding: 0 6px;

      .title-bold {
        font-weight: bold;
      }

      .title-gray {
        color: gray;
        font-size: 12px;
      }
    }
  }

  .btn-edit {
    border: 1px solid #a3a2a2;
    padding: 0 10px;
    line-height: 20px;
    height: 20px;
    border-radius: 6px;
    font-size: 12px;
  }

  .channel-item {
    font-size: 12px;
    text-align: center;
    line-height: 36px;
    background-color: #fafafa;
    margin: 6px;
  }

  .cross-badge {
    position: absolute;
    right: -3px;
    top: 0;
    border: none;
  }

  .sp-line {
    margin: 10px 0 20px 0;
  }
</style>
